import { BUNDLE_TYPE_ENUM } from "../base/EnumIndex";
import MaskItem from "../item/Common/MaskItem";
import LevelBase from "../LevelBase";
import { AudioManager1 } from "../manager/AudioManager1";

const { ccclass, property } = cc._decorator;

@ccclass
export default class rubOutItem extends cc.Component {
    @property({ type: MaskItem, displayName: "MaskItem脚本" })
    maskItem: MaskItem = null;
    /** 要执行Json里面配置的那个Tween */
    @property(cc.String)
    tweenStr: string = "";
    @property({ type: cc.Node, displayName: "要执行JsonTween的节点" })
    runTwNode: cc.Node = null;

    isClick: boolean = true;
    onLoad() {
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.touchMoveBack, this);
        this.node.on(cc.Node.EventType.TOUCH_START, this.touchStart, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.touchEnd, this);
    }
    onDestroy() {
        this.node.off(cc.Node.EventType.TOUCH_MOVE, this.touchMoveBack, this);
        this.node.off(cc.Node.EventType.TOUCH_START, this.touchStart, this);
        this.node.off(cc.Node.EventType.TOUCH_END, this.touchEnd, this);
    }
    async touchStart() {
        if (!this.isClick) {
            return
        }
        let audioId = await AudioManager1.playAudio("cazuozi", 3, BUNDLE_TYPE_ENUM.GAME);
        AudioManager1.setAudioLoop(audioId, true)
    }
    touchEnd() {
        if (!this.isClick) {
            return
        }
        AudioManager1.stopAudioForName("cazuozi");
    }
    touchMoveBack(touches: cc.Event.EventTouch) {
        if (!this.isClick) {
            return
        }
        this.maskItem.updateRubOut(touches.getLocation());
        if (this.maskItem.isWinRub) {
            AudioManager1.stopAudioForName("cazuozi");
            this.maskItem.rubNode.active = false;
            this.isClick = false;

            let levelBase = cc.director.getScene().getChildByName("Canvas").getComponentInChildren(LevelBase);
            if (levelBase && this.tweenStr != "") {
                levelBase.runNodeTween(this.runTwNode, levelBase.getInterTwForName(this.tweenStr).interTween);
            }
        }
    }
}
